package com.qidianit.mes.device.service.impl;

import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.device.mapper.DeviceMainPlanMapper;
import com.qidianit.mes.device.mapper.DeviceMaintainItemMapper;
import com.qidianit.mes.device.service.DeviceMainPlanService;
import com.qidianit.mes.device.vo.DeviceMainPlanOfItem;
import com.qidianit.mes.device.vo.DeviceMainPlanOfLedger;
import com.qidianit.mes.pojo.DeviceMainPlan;
import com.qidianit.mes.pojo.DeviceMaintainItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DeviceMainPlanServiceImpl implements DeviceMainPlanService {

    @Autowired
    DeviceMainPlanMapper deviceMainPlanMapper;
    @Override
    public MESResult list(String planCode, String planName, String planType, String planStatus, int page, int limit) {

        List<DeviceMainPlan> deviceMainPlans = deviceMainPlanMapper.list(planCode,planName,planType,planStatus);
        List<DeviceMainPlan> collect = deviceMainPlans.stream().map(item -> {
            String frequencyAmount = item.getFrequencyAmount();
            String frequencyUnit = item.getFrequencyUnit();
            item.setFrequency(frequencyAmount + '/'+frequencyUnit);
            return item;
        }).collect(Collectors.toList());

        return MESResult.success(collect);
    }

    @Override
    public MESResult add(DeviceMainPlan deviceMainPlan) {

        deviceMainPlan.setStatus("1");
        deviceMainPlanMapper.add(deviceMainPlan);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult edit(DeviceMainPlan deviceMainPlan) {
        deviceMainPlanMapper.edit(deviceMainPlan);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);

    }

    @Override
    public MESResult delete(String id) {
        return null;
    }

    @Override
    public MESResult listItemsByPlanId(String planCode, int page, int limit) {

            List<DeviceMaintainItem> deviceMainPlans = deviceMainPlanMapper.listItemsByPlanId(planCode);

            return MESResult.success(deviceMainPlans);
    }
    @Override
    public MESResult listledgersByPlanId(String planCode, int page, int limit) {

        List<DeviceMaintainItem> deviceMainPlans = deviceMainPlanMapper.listledgersByPlanId(planCode);

        return MESResult.success(deviceMainPlans);
    }

    @Override
    public MESResult addMainPlanOfItem(DeviceMainPlanOfItem mainPlanOfItem) {
        List<String> projectCode = mainPlanOfItem.getProjectCode();
        String planCode = mainPlanOfItem.getPlanCode();
        deviceMainPlanMapper.addMainPlanOfItem(projectCode,planCode);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult addMainPlanOfLedger(DeviceMainPlanOfLedger deviceMainPlanOfLedger) {
        List<String> deviceCode = deviceMainPlanOfLedger.getDeviceCode();
        String planCode = deviceMainPlanOfLedger.getPlanCode();
        deviceMainPlanMapper.addMainPlanOfLedger(deviceCode,planCode);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult deleteMainPlanOfItem(DeviceMainPlanOfItem mainPlanOfItem) {
        String planCode = mainPlanOfItem.getPlanCode();
        List<String> projectCode = mainPlanOfItem.getProjectCode();
        deviceMainPlanMapper.deleteMainPlanOfItem(projectCode,planCode);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult deleteMainPlanOfLedger(DeviceMainPlanOfLedger deviceMainPlanOfLedger) {
        String planCode = deviceMainPlanOfLedger.getPlanCode();
        List<String> deviceCode = deviceMainPlanOfLedger.getDeviceCode();
        deviceMainPlanMapper.deleteMainPlanOfLedger(deviceCode,planCode);
        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

}
